package com.huangsm.book.designmodel.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author huang
 * @PACKAGE_NAME com.huangsm.book.designmodel.nio
 * @PROJECT_NAME concurrence
 * @date 2019/2/18
 */
public class ReadNioDemo {
    /**
     * 通过Buffer读取文件
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        FileInputStream fs=new FileInputStream(new File("H:\\miao-common.rar"));
        //得到通道
        FileChannel fc = fs.getChannel();
        //NIO字节缓冲 容量为1024字节
        ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
        //将文件内容存入ByteBuffer中
        fc.read(byteBuffer);
        //关闭通道
        fc.close();
        //读取
        byteBuffer.flip();
    }
}
